import app
import requests


# 创建封装登录接口类
# class LoginApi():
#     def __init__(self):
#         self.img_verify_code_url = app.BASE_URL + "/common/public/verifycode1/{}"
#
#     # 获取图片验证码类
#     def get_img_verify_code(self, r):
#         url = self.img_verify_code_url.format(r)
#         return requests.get(url)


# 创建封装登录接口类
class LoginApi():
    def __init__(self, session):
        self.img_verify_code_url = app.BASE_URL + "/common/public/verifycode1/{}"
        self.info_verify_code_url = app.BASE_URL + "/member/public/sendSms"
        self.register_url = app.BASE_URL + "/member/public/reg"
        self.login_url = app.BASE_URL + "/member/public/login"
        self.whether_login_url = app.BASE_URL + "/member/public/islogin"

        self.session = session

    # 获取图片验证码类
    def get_img_verify_code(self, r):
        url = self.img_verify_code_url.format(r)
        return self.session.get(url)

    # 获取短信验证码
    def get_info_verify_code(self, phone, img_verify_code):
        data = {"phone": phone,
                "imgVerifyCode": img_verify_code,
                "type": "reg"}
        return self.session.post(self.info_verify_code_url, data=data)

    # 注册接口
    def register(self, phone, password, img_verify_code, phone_code, dy_server, invite_phone=None):
        data = {
            "password": password,
            "verifycode": img_verify_code,
            "phone_code": phone_code,
            "dy_server": dy_server
        }
        if phone is not None:
            data["phone"] = phone

        if invite_phone is not None:
            data["invite_phone"] = invite_phone

        return self.session.post(self.register_url, data)

    # 登录接口
    def login(self, keywords, password):
        params = {
            "keywords": keywords,
            "password": password
        }
        return self.session.post(self.login_url, data=params)

    # 是否登录接口
    def whetherif_login(self):
        return self.session.post(self.whether_login_url)
